'''主要是showData方法，传入历史适应度，可以画出、输出每一代适应度的平均值和最大值，输出适应度历史记录'''
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

'''显示实验历史数据'''
def showData(history_king, history_fitV, level, clickNum, size_pop, max_iter, pc, prob_mut):
    history_fitV_df = pd.DataFrame(history_fitV)
    fig, ax = plt.subplots(2, 1)
    plt_avg = history_fitV_df.mean(axis=1)
    plt_max = history_fitV_df.max(axis=1)
    best_fit = plt_max[ plt_max.argmax() ]
    best_operat = history_king[ plt_max.argmax() ]
    
    print('history_fitV:\n', history_fitV_df)
    print('avg fitV:\n', plt_avg)
    print('max fitV:\n', plt_max)
    print('history king:\n', pd.DataFrame(history_king))
    print('best score:\t', best_fit)
    print('best king:\t', best_operat)
    
    ax[0].set_title('level: {}        clickNum: {}      size_pop: {}        max_inter: {}       pc: {}      prob_mut: {}'.format(level, clickNum, size_pop, max_iter, pc, prob_mut), fontsize=15)
    ax[0].set_ylabel('average fit',fontsize=15)
    ax[0].plot(history_fitV_df.index, history_fitV_df.values, '.', color='red') # 红点点出所有个体适应度
    ax[0].plot(history_fitV_df.index, plt_avg)         # 每代平均个体适应度曲线
    ax[1].set_title('best_fit: {}       best_operat: {}'.format(best_fit, best_operat), fontsize=15)
    ax[1].set_ylabel('max fit',fontsize=15)
    ax[1].plot(plt_max.index, plt_max, label='max') # 每代最优个体适应度曲线
    # ax[1].plot(plt_max.index, plt_max.cummax())   # 每代累计最优个体适应度曲线（曲线不会下降，当下一代变小时保持当前最大值）
    plt.show()